package com.shopUser.contoller;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.shopAuth.feign.AppSessionFeign;
import com.shopAuth.feign.CheckAuthFeign;
import com.shopUser.bean.User;
import com.shopUser.service.IUserService;

import shopBean.common.bean.RestResult;
import shopBean.common.responseCode.Status;
import shopBean.common.utils.RestResultGenerator;
import shopBean.common.utils.Tool;

@RestController
@RequestMapping("/user/")
@SuppressWarnings("rawtypes")
public class UserContoller {

	@Autowired
	IUserService userService;
	@Autowired
	CheckAuthFeign checkAuthFeign;
	@Autowired
	AppSessionFeign appSessionFeign ;

	@RequestMapping("getByAccount.api")
	public RestResult<User> getByAccountApi(String account) {
		return RestResultGenerator.genSuccessResult(this.userService.getByAccount(account));
	}
	@RequestMapping("get.api")
	public RestResult<User> get(String id) {

		return RestResultGenerator.genSuccessResult(this.userService.get(id));
	}
	
	@RequestMapping("reg.do")
	public RestResult<User> reg(@RequestParam(required = true) String account, @RequestParam(required = true) String password,
			@RequestParam(required = true) String code,HttpServletRequest request) {
		if(!shopBean.common.utils.PhoneFormatCheckUtils.isChinaPhoneLegal(account)) {
			return RestResultGenerator.genErrorResult("手机号不正确", null, Status.tel_invalid);
		}
		if(!Tool.checkPassword(password)) {
			return RestResultGenerator.genErrorResult("密码由8-16位数字或这字母组成", null, Status.password_error);
		}
		RestResult restResult = this.checkAuthFeign.checkCode(account, code, true);
		if (restResult == null || !Status.sys_success.equals(restResult.getStatusCode())
				|| restResult.getData().toString().equals("false")) {
			return RestResultGenerator.genErrorResult("验证码不正确", null, Status.code_is_error);
		}
		if(this.userService.getByAccount(account) != null) {
			return RestResultGenerator.genErrorResult("该账号已注册", null, Status.user_is_exits);
		}
		User user = new User(account, password);
		this.userService.insert(user);
		user = this.userService.getByAccount(account);
		RestResult appsession = appSessionFeign.createSession(user.getId(), Tool.getIpAddr(request));
		if(appsession == null  || !Status.sys_success.equals(appsession.getStatusCode()) ) {
			return RestResultGenerator.genErrorResult("已注册，登录失败", null, Status.login_error);
		}
		
		RestResult<User> returnRestResult = RestResultGenerator.genSuccessResult(user);
		returnRestResult.setData1(appsession.getData());
		return returnRestResult;
		
	}
	//没有结尾的都是需要登录
	//.do 是不需要登录的
	//.api 云服务内部调用的服务
	@RequestMapping("getByUserId")
	public RestResult<User> getByUserId(String userId) {
		if (StringUtils.isBlank(userId)) {
			return RestResultGenerator.genErrorResult("手机号不为空", null, Status.tel_is_empty);
		}

		return RestResultGenerator.genSuccessResult(this.userService.get(userId));
	}
	@RequestMapping("login.do")
	public RestResult<User> login(@RequestParam(required = true) String account, @RequestParam(required = true) String password,HttpServletRequest request) {
		RestResult<User> result = this.userService.login(account, password);
		
		
		if(result.getStatusCode().equals(Status.sys_success)) {
			RestResult appsession = appSessionFeign.createSession(account,  Tool.getIpAddr(request));
			if(appsession == null  || !Status.sys_success.equals(appsession.getStatusCode()) ) {
				return RestResultGenerator.genErrorResult("登录失败", null, Status.login_error);
			}
			result.setData1(appsession.getData());
		}
		return result;
	}
}
